<template>
  <div>

    <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
      <el-col>
        <span>{{ treeNode.name }}</span>
      </el-col>
      <el-col :span="4">
        <el-row type="flex">
          <el-col>{{ treeNode.manager }}</el-col>
          <el-col>
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>

              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add" :disabled="!checkPermission('add-depts')"><span>添加子部门<i class="el-icon-arrow-down"></i></span></el-dropdown-item>
                <!-- 使用点击事件 .native冒泡到父组件 -->
                <!-- <el-dropdown-item v-if="!isRoot" @click.native="handleEdit"><span>编辑部门<i class="el-icon-arrow-down"></i></span></el-dropdown-item> -->
                <el-dropdown-item v-if="!isRoot" :disabled="!checkPermission('edit-depts')" command="edit"><span>编辑部门<i class="el-icon-arrow-down"></i></span></el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" :disabled="!checkPermission('delete-depts')" command="delete"><span>删除部门<i class="el-icon-arrow-down"></i></span></el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { delDepmentById } from '@/api/departments'
export default {
  name: 'TreeTools',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

    }
  },
  created() {

  },
  methods: {
    // 方案1
    handleCommand(command) {
      // 添加子部门
      if (command === 'add') {
        this.$emit('adddepts', this.treeNode)
      } else if (command === 'edit') {
        // 编辑部门
        console.log('edit')
        this.$emit('editDeps', this.treeNode)
      } else if (command === 'delete') {
        // 删除部门
        console.log('delete')
        this.$confirm('您确定要删除部门吗?').then(async() => {
          await delDepmentById(this.treeNode.id)
          this.$message.success('删除成功!')
          this.$emit('delbumen')
        })
      }
    },
    // 方案2
    handleEdit() {
      console.log('点击编辑')
    },
    adddepts(node) {

    }

  }
}
</script>

<style lang="less" scoped>

</style>
